package com.sgb.kjwl.view.ui.adapter.base

abstract class HDBaseAdapterEx<T> : HDBaseAdapter<SimpleViewHolder>(),
        RecyclerContract.SimpleContract<MutableList<T>,T>,
        RecyclerContract.SpecificationContract<MutableList<T>,T> {
    var data = mutableListOf<T>()

    /**
     * 刷新
     */
    override fun onRefresh(ts: MutableList<T>) {
        data.clear()
        data.addAll(ts)
        notifyDataSetChanged()
    }

    /**
     * 获取Item
     */
    override fun getItem(position: Int): T? {
        if (data.includeIndex(position)){
            return data[position]
        }
        return null
    }

    /**
     * 末尾添加多条
     */
    override fun onLoad(ts: MutableList<T>) {
        val listSize = ts.size
        data.addAll(ts)
        notifyItemRangeInserted(data.size - listSize,listSize)
    }

    /**
     * 指定位置插入多条
     */
    override fun addAll(positionStart: Int, ts: MutableList<T>) {
        val listSize = ts.size
        data.addAll(positionStart,ts)
        notifyItemRangeInserted(positionStart,listSize)
    }

    /**
     * 插入
     */
    override fun insertItem(t: T, position: Int) {
        data.add(position,t)
        notifyItemInserted(position)
    }

    /**
     * 删除Item
     */
    override fun deleteItem(position: Int) {
        data.removeAt(position)
        notifyItemRemoved(position)
    }

    /**
     * 替换 Item
     */
    override fun replaceItem(t: T, position: Int) {
        data[position] = t
        notifyItemChanged(position)
    }

    override fun getDataItemCount(): Int {
        return data.size
    }

}